package work.dp;

/**
 * @author WDH
 * @description
 * @date 2024年12月03日 14:48
 */

public class Problem63 {

    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
        int m = obstacleGrid.length;
        int n = obstacleGrid[0].length;
        int[][] dp = new int[m][n];
        for (int i = 0; i < m; i++) {
            dp[i][0] = obstacleGrid[i][0] == 0 ? 1 : 0;
        }

        for (int i = 0; i < n; i++) {
            dp[0][i] = obstacleGrid[0][i] == 0 ? 1 : 0;
        }
        for (int i = 1; i < m; i++) {
            for (int j = 1; j < n; j++) {
                dp[i][j] = obstacleGrid[i][j] == 0 ? dp[i - 1][j] + dp[i][j - 1] : 0;
            }
        }
        return dp[m - 1][n - 1];
    }

    public static void main(String[] args) {
        int[][] obstacleGrid = {
                {0, 0, 0},
                {0, 1, 0},
                {0, 0, 0},
                {0, 0, 0},
        };

        Problem63 problem63 = new Problem63();
        int result = problem63.uniquePathsWithObstacles(obstacleGrid);
        System.out.println("Unique paths with obstacles: " + result);
    }
}
